package com.dhcc.wb.user;

import java.util.List;
import java.util.UUID;

import org.json.JSONObject;

import com.dhcc.common.util.StringUtil;
import com.dhcc.wb.gongzhonghao.GzhDao;
import com.dhcc.wb.gongzhonghao.GzhModel;
import com.dhcc.wb.util.HttpUtil;
import com.dhcc.wb.util.TokenUtil;
import com.google.gson.Gson;

/**
 * @author wb
 *
 */
public class UserTask{
	/**
	 * 用户分析
	 */
	private static String getusersummaryUrl = "https://api.weixin.qq.com/datacube/getusersummary?access_token=";//获取用户增减数据
	private static String getusercumulateUrl = "https://api.weixin.qq.com/datacube/getusercumulate?access_token=";//获取累计用户数据


	
	//更新所有公众号用户
	public static void updateUser(String begin_date,String end_date){
		GzhDao gzhdao = new GzhDao();
		List<GzhModel> gzhlist = gzhdao.getAll();
		if(gzhlist != null && gzhlist.size()>0){
			for(GzhModel gzh :gzhlist){
				updateSingleUser(gzh.getGzhid(),begin_date,end_date,gzh.getGzhname());
				updateSingleUser1(gzh.getGzhid(),begin_date,end_date,gzh.getGzhname());
			}
		}
	}
	
	
	//更新一个公众号用户，只是更新 getusersummaryUrl
	public static void updateSingleUser(String gzhid,String begin_date,String end_date,String gzhname){
		try {
			String token = TokenUtil.getToken(gzhid);
			if(!StringUtil.isNullOrEmpty(token)){
				JSONObject json = new JSONObject();
				json.put("begin_date", begin_date);
				json.put("end_date", end_date);
//				System.out.println("token:"+token);
				String content1 = HttpUtil.postContent(getusersummaryUrl+token, json.toString());
				Gson gson2 = new Gson();
				UserListModel userlist1 = gson2.fromJson(content1, UserListModel.class);
				if(null != userlist1 && null != userlist1.getList() && userlist1.getList().size()>0){
					for(UserModel user:userlist1.getList()){
						Boolean a = user.getNew_user() ==0 && user.getCancel_user() ==0;
						if(!a){
							UserDao dao = new UserDao();
							UserModel user1 = dao.queryqiye_result(gzhid, user.getRef_date());
							if(user1 == null){//如果数据库不存在该数据
								dao.addUser1(UUID.randomUUID().toString(), user.getRef_date(), user.getUser_source(), user.getNew_user(), user.getCancel_user(), gzhid,gzhname);
							}else{//如果数据库存在
								dao.updateUser1(user1.getIds(), user.getUser_source(), user.getNew_user(), user.getCancel_user(),gzhname);
							}
						}
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
	}
	
	//更新一个公众号用户，只是更新 getusersummaryUrl
	public static void updateSingleUser1(String gzhid,String begin_date,String end_date,String gzhname){
		try {
			String token = TokenUtil.getToken(gzhid);
			if(!StringUtil.isNullOrEmpty(token)){
				JSONObject json = new JSONObject();
				json.put("begin_date", begin_date);
				json.put("end_date", end_date);
				
				String content1 = HttpUtil.postContent(getusercumulateUrl+token, json.toString());
				Gson gson2 = new Gson();
				UserListModel userlist1 = gson2.fromJson(content1, UserListModel.class);
				if(userlist1 != null && userlist1.getList() != null && userlist1.getList().size()>0){
					for(UserModel user:userlist1.getList()){
							UserDao dao = new UserDao();
							UserModel user1 = dao.queryqiye_result(gzhid, user.getRef_date());
							if(user1 == null){//如果数据库不存在该数据
								dao.addUser2(UUID.randomUUID().toString(), user.getRef_date(), user.getCumulate_user(), gzhid,gzhname);
							}else{//如果数据库存在
								dao.updateUser2(user1.getIds(), user.getCumulate_user(),gzhname);
							}
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		
	}
	
	
	 public static void main(String[] args) {
	    	updateUser("2019-11-21","2019-11-21");
		}
    
		
		
	
}
